#include "gd32w51x.h"
#include "systick.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "version.h"

#include "DevicesQueue.h"
#include "DevicesTime.h"
#include "DevicesSoftTimer.h"
#include "DevicesKey.h"

#include "microList.h"
#include "microTask.h"
#include "microTime.h"
#include "microTimer.h"
#include "microSemaphore.h"

#include "userTask.h"


MicroTaskType *g_ptypeTaskLedHandle = NULL;

MicroSemaphoreType *g_ptypeSemaphoreKey = NULL;


void vTaskLed(void *pvValue)
{
    MicroTaskType *g_ptypeTaskNow = pvValue;

    printf("\n\r%s task start.\r\n", ((MicroTaskType *)pvValue)->name);

    if(g_ptypeTaskNow->timer.timeDuration == 0)
        vMicroTaskDelay(1000000);
    else
        vMicroTaskDelayUntil(1000000);
}

void vTaskPrintf(void *pvValue)
{
    MicroTaskType *g_ptypeTaskNow = pvValue;

    printf("\n\r%s task start.\r\n", ((MicroTaskType *)pvValue)->name);

    cMicroSemaphoreReceive(g_ptypeSemaphoreKey);
}

void vTaskKey(void *pvValue)
{
    MicroTaskType *g_ptypeTaskNow = pvValue;
    KeyTypeDef typeKeyData = {0};
    int16_t sTime = 0;
    int8_t cNECState = 0;

    /* 等待消息 */
    // rt_sem_take(g_SemKey, RT_WAITING_FOREVER);

    /* 按键输入 */
    while(enumQueuePopDatas(&g_TypeQueueKeyInput, (uint8_t *)&typeKeyData, sizeof(KeyTypeDef)) == queueNormal)
    {
        /* 机械按键 */
        if(typeKeyData.valuePress & KEY_RESET)
        {
        //     if(typeKeyData.state == (keyShort | keyCut))
        //         cSoftTimerReset(&st_softTimerINPUT_Select);
        //     else if(typeKeyData.state == keyLong)
        //         cSoftTimerReset(&st_softTimerINPUT_Start);

            printf("KEY_RESET.\r\n");

            cMicroSemaphoreSend(g_ptypeSemaphoreKey, typeKeyData.valuePress);
        }

        /* 触摸按键1 */
        if(typeKeyData.valuePress & KEY_TOUCH1)
        {
        }

        /* 触摸按键2 */
        if(typeKeyData.valuePress & KEY_TOUCH2)
        {
        }
    }
}

int8_t cTaskInit(void)
{
    g_ptypeSemaphoreKey = ptypeMicroSemaphoreCreat(0);


    g_ptypeTaskLedHandle = ptypeMicroTaskGreat("LED", 0, vTaskLed);
    if(g_ptypeTaskLedHandle == NULL)
    {
        return 1;
    }

    g_ptypeTaskLedHandle = ptypeMicroTaskGreat("Key", 2, vTaskKey);
    if(g_ptypeTaskLedHandle == NULL)
    {
        return 1;
    }

    g_ptypeTaskLedHandle = ptypeMicroTaskGreat("Printf", 1, vTaskPrintf);
    if(g_ptypeTaskLedHandle == NULL)
    {
        return 1;
    }

    return 0;
}
